import Charts from "../../../../utils/wxcharts"
import util from "../../../../utils/util";
const app = getApp();
var price, hit;
var options = {
    dataPointShape: "circle",
    categories: [],
    type: 'line',
    xAxis: {
        type: "calibration",
    },
    yAxis: {
        title: '成交金额 (元)',
        min: 0,
        gridColor: "#F1F1F1",
    },
    width: 325,
    height: 240,
    dataLabel: true,
    animation: false,
};
var halfyear = [];
var fullyear = [];
var xType = 0; // 0：半年；1：一年；
var dataList = [];
Page({

    /**
     * 页面的初始数据
     */
    data: {
        info: [],
        cityIndex: 0,
        type: ["摇号中签"],
        typeIndex: 0,
        tabIndex: 0,
        result: "",
        date: [],
        dateIndex: 0,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {
        util.request('zhuanti.php', {
            action: "yh_qi"
        }, ).then(res => {
            if (res.data.message == "success") {
                this.setData({
                    info: res.data.data,
                    date: res.data.data[0].qi
                });
                this.getCityData();
            }
        });

        let month = new Date().getMonth() + 1;
        let year = new Date().getFullYear();
        for (let i = month; i >= month - 5; i--) {
            if (i > 0)
                halfyear.unshift(parseInt(`${year}${util.formatNumber(i)}`));
            else if (i == 0)
                halfyear.unshift(parseInt(`${year - 1}${util.formatNumber(12 - i )}`));
            else if (i < 0)
                halfyear.unshift(parseInt(`${year - 1}${util.formatNumber(12 + i)}`));
        }

        for (let i = month; i > month - 12; i--) {
            if (i > 0)
                fullyear.unshift(parseInt(`${year}${util.formatNumber(i)}`));
            else if (i == 0)
                fullyear.unshift(parseInt(`${year - 1}${util.formatNumber(12 - i )}`));
            else if (i < 0)
                fullyear.unshift(parseInt(`${year - 1}${util.formatNumber(12 + i)}`));
        }

        options.categories = halfyear;
    },

    getCityData() {
        let year = new Date().getFullYear();
        let month = util.formatNumber(new Date().getMonth() + 1);
        let qiList = this.data.info[this.data.cityIndex].qi;
        this.setData({
            lastQi: qiList[qiList.length - 1],
        });
        util.request('zhuanti.php', {
            action: "yaohaochaxun",
            citycode: this.data.info[this.data.cityIndex].citycode,
            qi: `${year}${month}`,
        }, ).then(res => {
            if (res.data.message == "success") {
                dataList = res.data.data;
                this.mackCircle("price");
            }
        });
    },

    mackCircle(type = "price") {

        let perMin = [];
        let comMin = [];
        let perRate = [];
        let comRate = [];
        let categories = [];
        if (dataList) {
            halfyear.forEach(date => {
                categories = halfyear;
                if (dataList.year.some(item => item.qi == date)) {
                    perMin.push(dataList.halfyear.filter(item => item.qi == date)[0].price.per_min);
                    comMin.push(dataList.halfyear.filter(item => item.qi == date)[0].price.com_min);
                    perRate.push(dataList.halfyear.filter(item => item.qi == date)[0].rate.per_rate);
                    comRate.push(dataList.halfyear.filter(item => item.qi == date)[0].rate.com_rate);
                } else {
                    perMin.push(0);
                    comMin.push(0);
                    perRate.push(0);
                    comRate.push(0);
                }

            });
        }

        if (type == "price") {
            price = new Charts({
                canvasId: 'price',
                series: [{
                    name: '个人最低成交价',
                    data: perMin,
                    color: "#F5C27E"
                }, {
                    name: '单位最低成交价',
                    data: comMin,
                    color: "#9CCC52"
                }],
                ...options,
            });
        } else if (type == "hit") {
            hit = new Charts({
                canvasId: 'hit',
                series: [{
                    name: '个人中签率',
                    data: perRate,
                    color: "#F5C27E",
                }, {
                    name: '单位中签率',
                    data: comRate,
                    color: "#9CCC52",
                    format: val => `${val}%`
                }],
                ...options,
                yAxis: {
                    title: '中签率(%)',
                },
            });
        }
    },

    tabBarClick(e) {
        let tabIndex = e.currentTarget.dataset.index;
        this.setData({
            tabIndex,
        });
        this.mackCircle(tabIndex == 0 ? "price" : "hit");
    },

    changeCity(e) {
        let cityIndex = e.detail.value;
        this.setData({
            cityIndex,
            tabIndex: 0,
            result: "",
        });
        this.setData({
            date: this.data.info[this.data.cityIndex].qi,
            dateIndex: 0,
        });
        this.getCityData();
    },

    changeDate(e) {
        let dateIndex = e.detail.value;
        this.setData({
            dateIndex,
            result: ""
        });
    },

    getDetailInfo() {
        let perMin = [];
        let comMin = [];
        let perRate = [];
        let comRate = [];
        let categories = [];
        if (xType == 0) {
            halfyear.forEach(date => {
                categories = halfyear;
                if (dataList.halfyear.some(item => item.qi == date)) {
                    perMin.push(dataList.halfyear.filter(item => item.qi == date)[0].price.per_min);
                    comMin.push(dataList.halfyear.filter(item => item.qi == date)[0].price.com_min);
                    perRate.push(dataList.halfyear.filter(item => item.qi == date)[0].rate.per_rate);
                    comRate.push(dataList.halfyear.filter(item => item.qi == date)[0].rate.com_rate);
                } else {
                    perMin.push(0);
                    comMin.push(0);
                    perRate.push(0);
                    comRate.push(0);
                }

            });
        } else if (xType == 1) {
            fullyear.forEach(date => {
                categories = fullyear;
                if (dataList.year.some(item => item.qi == date)) {
                    perMin.push(dataList.year.filter(item => item.qi == date)[0].price.per_min);
                    comMin.push(dataList.year.filter(item => item.qi == date)[0].price.com_min);
                    perRate.push(dataList.year.filter(item => item.qi == date)[0].rate.per_rate);
                    comRate.push(dataList.year.filter(item => item.qi == date)[0].rate.com_rate);
                } else {
                    perMin.push(0);
                    comMin.push(0);
                    perRate.push(0);
                    comRate.push(0);
                }

            });
        }
        return { perMin, comMin, perRate, comRate, categories };
    },

    select(e) {
        let qi = this.data.date[e.detail.value.date];
        util.request('zhuanti.php', {
            action: "yaohaochaxun",
            citycode: this.data.info[this.data.cityIndex].citycode,
            qi,
        }, ).then(res => {
            if (res.data.message == "success")
                this.setData({
                    result: res.data.data.yh,
                });
            else
                this.setData({
                    result: "",
                });
            this.getCityData();
        });

    },

    shrink(e) {
        let type = e.currentTarget.dataset.type == 0 ? "price" : "hit";
        xType = 0;
        this.mackCircle(type);
    },

    big(e) {
        let type = e.currentTarget.dataset.type == 0 ? "price" : "hit";
        xType = 1;
        if (type == "price") {
            price.updateData({
                categories: this.getDetailInfo().categories,
                series: [{
                    name: '个人最低成交价',
                    data: this.getDetailInfo().perMin,
                    color: "#F5C27E"
                }, {
                    name: '单位最低成交价',
                    data: this.getDetailInfo().comMin,
                    color: "#9CCC52"
                }],
            });
        } else if (type == "hit") {
            hit.updateData({
                categories: this.getDetailInfo().categories,
                series: [{
                    name: '个人中签率',
                    data: this.getDetailInfo().perRate,
                    color: "#F5C27E",
                }, {
                    name: '单位中签率',
                    data: this.getDetailInfo().comRate,
                    color: "#9CCC52",
                    format: val => `${val}%`
                }],
            });
        }
    },

    onShareAppMessage: function() {

    }
})